package com.fancon.android.emoji.keyboard;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.text.Html;
import android.text.Spanned;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;

import com.fancon.android.emoji.Emoji;
import com.fancon.android.emoji.EmojiDockPanel.EmojiDockListener;
import com.fancon.android.emoji.EmojiDrawableGetter;
import com.fancon.android.emoji.EmojiKeyBoard.EmojiKeyBoardListener;
import com.fancon.android.emoji.EmojiUtil;
import com.fancon.android.emoji.R;
import com.fancon.android.emoji.widget.EmojiRadioButton;
import com.fancon.android.emoji.widget.M05RichText;

public class M05FloatPanel extends BetterPopupWindow implements OnClickListener {
	private EmojiDockListener listener;
	private EmojiRadioButton radioSmiley, radioBell, radioVehicle, radioFlower,
			radioNumber;
	protected M05RichText richEditText;
	private M05FloatKeyBoard keyBoard;
	private View currentSelected;
	
	public M05FloatPanel(View anchor, M05RichText richEditText) {
		super(anchor);
		this.richEditText = richEditText;
		// TODO Auto-generated constructor stub
	}

	public M05FloatPanel(View anchor, M05RichText richEditText,
			EmojiDockListener listener) {
		super(anchor);
		// TODO Auto-generated constructor stub
		this.richEditText = richEditText;
		this.listener = listener;
	}

	protected void onCreate() {
		// inflate layout
		LayoutInflater inflater = (LayoutInflater) this.anchor.getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.emoji_panel,
				null);
		radioSmiley = (EmojiRadioButton) root.findViewById(R.id.radioSmiley);
		radioBell = (EmojiRadioButton) root.findViewById(R.id.radioBell);
		radioVehicle = (EmojiRadioButton) root.findViewById(R.id.radioVehicle);
		radioFlower = (EmojiRadioButton) root.findViewById(R.id.radioFlower);
		radioNumber = (EmojiRadioButton) root.findViewById(R.id.radioNumber);

		radioSmiley.setOnClickListener(this);
		radioBell.setOnClickListener(this);
		radioVehicle.setOnClickListener(this);
		radioFlower.setOnClickListener(this);
		radioNumber.setOnClickListener(this);

		this.setContentView(root);
	}

	public void showPanel(int keyboardHeight) {
		this.preShow();
		this.window.setFocusable(false);
		this.window.setTouchable(true);
		this.window.setOutsideTouchable(false);

		this.window.setAnimationStyle(R.style.Animations_GrowFromBottom);
		int[] location = new int[2];
		this.anchor.getLocationOnScreen(location);
		this.root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		// int screenWidth = this.windowManager.getDefaultDisplay().getWidth();
		int screenHeight = this.windowManager.getDefaultDisplay().getHeight();
		this.window.showAtLocation(this.anchor, Gravity.TOP, 0, screenHeight
				- keyboardHeight - this.root.getMeasuredHeight());
	}

	public void showFloatPanel(int keyboardHeight) {
		this.preShow();
		this.window.setFocusable(false);
		this.window.setTouchable(true);
		this.window.setOutsideTouchable(false);

		this.window.setAnimationStyle(R.style.Animations_GrowFromBottom);
		int[] location = new int[2];
		this.anchor.getLocationOnScreen(location);
		this.root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		// int screenWidth = this.windowManager.getDefaultDisplay().getWidth();
		int screenHeight = this.windowManager.getDefaultDisplay().getHeight();
		this.window.showAtLocation(this.anchor, Gravity.TOP, 0,
				screenHeight - keyboardHeight - this.root.getMeasuredHeight()
						- this.anchor.getMeasuredHeight());
	}

	public EmojiDockListener getDockListener() {
		return listener;
	}

	public void getDockListener(EmojiDockListener listener) {
		this.listener = listener;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v instanceof RadioButton) {
			if (v == radioSmiley) {
				openEmojiKeyBoard(EmojiUtil.SMILEY, v);
			} else if (v == radioBell) {
				openEmojiKeyBoard(EmojiUtil.BELL, v);
			} else if (v == radioVehicle) {
				openEmojiKeyBoard(EmojiUtil.VEHICLE, v);
			} else if (v == radioFlower) {
				openEmojiKeyBoard(EmojiUtil.FLOWER, v);
			} else if (v == radioNumber) {
				openEmojiKeyBoard(EmojiUtil.NUMBER, v);
			}
			currentSelected = v;
			if (listener != null) {
				listener.onItemSelected(v);
			}
			onSelectedChange();
		}
	}

	private void onSelectedChange() {
		radioSmiley.setBackgroundResource(R.drawable.nonselect);
		radioBell.setBackgroundResource(R.drawable.nonselect);
		radioVehicle.setBackgroundResource(R.drawable.nonselect);
		radioFlower.setBackgroundResource(R.drawable.nonselect);
		radioNumber.setBackgroundResource(R.drawable.nonselect);
		currentSelected.setBackgroundResource(R.drawable.selected);
		//((RadioButton)currentSelected).setChecked(true);
	}
	public void openEmojiKeyBoard(int category, View v) {
		final EmojiKeyBoardListener listener = new EmojiKeyBoardListener() {
			@Override
			public void onEmojiSelected(View view) {
				// TODO Auto-generated method stub
				String code = ((Emoji) view).getCode();
				EmojiDrawableGetter imgGetter = richEditText.getmEmojiUtil().getImageGetter();
				//imgGetter.setmContext(anchor.getContext());
				Spanned cs = Html.fromHtml("<img src='" + code + "'/>", imgGetter, null);
				int start = richEditText.getSelectionStart();
				EmojiUtil.insertSpannedTextToCurrentPosition(cs, richEditText,
						start, start);
			}

			@Override
			public void afterClose(View v) {
				// TODO Auto-generated method stub
				showKeyBoard();
			}
		};
		richEditText.hideKeyBoard();
		// dismiss();
		if (keyBoard == null) {
			keyBoard = new M05FloatKeyBoard(this.anchor, false, category,
					(EmojiKeyBoardListener) listener);
			keyBoard.showFloatKeyBoard();
		}else{
			keyBoard.loadEmoji(category);
			if(keyBoard.isHide()){
				keyBoard.showFloatKeyBoard();
			}
		}
	}
	public void hideEmojiKeyBoard(){
		if(keyBoard != null){
			keyBoard.dismiss();
		}
	}
	public void showKeyBoard() {
		InputMethodManager inputMgr = (InputMethodManager) this.anchor
				.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		inputMgr.toggleSoftInput(0, 0);

	}

	public void showPanelOnTop() {
		this.preShow();
		this.window.setFocusable(false);
		this.window.setTouchable(true);
		this.window.setOutsideTouchable(false);

		this.window.setAnimationStyle(R.style.Animations_GrowFromBottom);
		int[] location = new int[2];
		this.anchor.getLocationOnScreen(location);
		this.root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		// int screenWidth = this.windowManager.getDefaultDisplay().getWidth();
		//int screenHeight = this.windowManager.getDefaultDisplay().getHeight();
		this.window.showAtLocation(this.anchor, Gravity.TOP, 0,
				getStatusBarHeight());
	}

	public int getStatusBarHeight() {
		Rect rectgle = new Rect();
		Window window = ((Activity) this.anchor.getContext()).getWindow();
		window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
		int StatusBarHeight = rectgle.top;
		//int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT)
		//		.getTop();
		//int TitleBarHeight = contentViewTop - StatusBarHeight;
		return StatusBarHeight;
	}

	public M05FloatKeyBoard getKeyBoard() {
		return keyBoard;
	}

	public void setKeyBoard(M05FloatKeyBoard keyBoard) {
		this.keyBoard = keyBoard;
	}
	
}
